import { ChangeDetectorRef, AfterViewInit, Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { STComponent } from '@delon/abc/st';
import { _HttpClient, SettingsService, User } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';

@Component({
  selector: 'app-business-order-set-purchase-edit',
  templateUrl: './set-purchase-edit.component.html'
})
export class BusinessOrderSetPurchaseEditComponent implements OnInit, AfterViewInit {
  record: any = {};
  i: any;
  validateForm!: FormGroup;
  controlsConfig = {
    ID: [null, null],
    link: ['', [Validators.required]],
    type: ['', [Validators.required]],
    isDefault: [0, [Validators.required]],
    sort: [0, []],
    itemId: ['', []],
    skuId: ['', []],
    orderSn: ['', []],
    orderId: ['', []]
  };
  purchase_types = [];
  parent_cdr: any;

  constructor(
    private modal: NzModalRef,
    private msgSrv: NzMessageService,
    public http: _HttpClient,
    private fb: FormBuilder,
    private settingService: SettingsService,
    private cdr: ChangeDetectorRef
  ) {
    this.loadDataFromServer();
    this.validateForm = this.fb.group(this.controlsConfig);
  }

  ngOnInit(): void {
    console.log(this.i, this.record);
    console.log('purchase_types', this.purchase_types);
    if (!this.purchase_types) {
      this.http.get(`api/purchase_types`).subscribe(res => {
        this.purchase_types = res.data;
        console.log('purchase_types', this.purchase_types);
      });
    }
    let id = this.i.id || this.record.ID;
    console.log(id);
    if (id > 0) {
      this.http.get(`api/purchases/${id}`).subscribe(res => {
        this.validateForm.patchValue(res.data);
        this.record = res.data;

        // this.controlsConfig.ID = [this.record.ID, null];
        // this.controlsConfig.link = [this.record.link, [Validators.required]];
        // this.controlsConfig.type = [this.record.type, [Validators.required]];
        // this.controlsConfig.isDefault = [this.record.isDefault, [Validators.required]];
        // this.controlsConfig.sort = [this.record.sort, []];
        // this.controlsConfig.itemId = [this.record.itemId, []];
        // this.controlsConfig.skuId = [this.record.skuId, []];
        // this.controlsConfig.orderSn = [this.record.orderSn, []];
        // this.controlsConfig.orderId = [this.record.orderId, []];

        //this.cdr.detectChanges();
      });
    } else {
      //this.record.shopId = this.i.shopId;
      this.record.orderSn = this.i.orderSn;
      this.record.orderId = this.i.orderId;
      this.record.itemId = this.i.itemId;
      this.record.skuId = this.i.skuId;
      this.record.sort = 0;
      this.record.link = '';
      this.record.isDefault = 0;
      this.record.type = 0;

      // this.controlsConfig.ID = [null, null];
      // this.controlsConfig.link = [this.record.link, [Validators.required]];
      // this.controlsConfig.type = [this.record.type, [Validators.required]];
      // this.controlsConfig.isDefault = [this.record.isDefault, [Validators.required]];
      // this.controlsConfig.sort = [this.record.sort, []];
      // this.controlsConfig.itemId = [this.record.itemId, []];
      // this.controlsConfig.skuId = [this.record.skuId, []];
      // this.controlsConfig.orderSn = [this.record.orderSn, []];
      // this.controlsConfig.orderId = [this.record.orderId, []];
      // console.log(this.i);
      // console.log(this.record);
      // console.log(this.controlsConfig);
    }
    this.cdr.detectChanges();
  }

  loadDataFromServer() {}

  save(value: any): void {
    console.log(value);
    if (value.ID) {
      this.http.put(`api/purchases/${value.ID}`, value).subscribe(res => {
        if (res.err_code != 0) {
          this.msgSrv.error(res.msg);
          return;
        }
        this.http.get(`api/purchases`).subscribe(res => {
          if (this.parent_cdr) {
            this.parent_cdr.detectChanges();
          }
          this.cdr.detectChanges();
          this.msgSrv.success(res.msg);
          this.modal.close(true);
        });
      });
    } else {
      this.http.post(`api/purchases`, value).subscribe(res => {
        if (res.err_code != 0) {
          this.msgSrv.error(res.msg);
          return;
        }
        this.http.get(`api/purchases`).subscribe(res => {
          if (this.parent_cdr) {
            this.parent_cdr.detectChanges();
          }
          this.cdr.detectChanges();
          this.msgSrv.success(res.msg);
          this.modal.close(true);
        });
      });
    }
  }

  ngAfterViewInit(): void {}

  close(): void {
    this.modal.destroy();
  }
}
